/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.app.utils;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;
import org.apache.log4j.Logger;

import javax.persistence.Persistence;
import javax.persistence.EntityManager;
/**
 *
 * @author KM
 */
public class PropertyUtils {

    private static Logger logger = Logger.getLogger(PropertyUtils.class);
    static Properties properties = new Properties();
    FileInputStream fis;
    
    static{
        initProperties();
    }

    public static boolean initProperties() {
        try {
            properties.load(new FileInputStream(PropertiesHelper.PROPERTY_FILE_PATH));
            
            return true;
        } catch (IOException e) {
            logger.error(e);
        }
        return false;
    }
    
    public static EntityManager initDB(){
        return Persistence.createEntityManagerFactory("Auto-download-moviePU").createEntityManager();
    }

    public static String getProperty(String name) {
        return properties.get(name).toString();
    }

    public static void setProperty(String name, String value) {
        properties.setProperty(name, value);
        saveProperty();        
    }
    
    public static void saveProperty(){
        try{
            properties.store(new FileOutputStream(PropertiesHelper.PROPERTY_FILE_PATH), null);
        }catch(Exception ex){
            logger.info("Unable to save property: " + ex.getMessage());
        }
    }
    
    public static void main(String[] args){
        String hello = PropertyUtils.getProperty(PropertiesHelper.GOOGLE_SEARCH_RESULTS);
        logger.info(hello);
        setProperty("Thang", "abc");
    }
}
